// @flow
import React from 'react'
import { Modal } from 'antd'
// import AgGrid from 'components/AgGrid'
import styles from './Monitor.css'
import Point from 'components/PointAll'
import VarComp from 'components/VarCompAll'
import Limits from 'components/LimitsAll'

import universalFetch, { handleFetchError } from 'store/modules/fetch'
type Props = {
}
type States = {
  visibleLimit: boolean,
  pointData: Array<any>,
  varData: Array<any>,
  limitsData: Array<Object>,
  monitorPointId: string,
  variableId: string
}
class MonitorPage extends React.Component {
  state: States
  getPointData: Function
  getPointVars: Function
  getVarData: Function
  getLimitsData: Function
  getVarLimits: Function
  handleLimitCancel: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visibleLimit: false,
      limitsData: [],
      varData: [],
      pointData: [],
      monitorPointId: '',
      variableId:''
    }
    this.getPointData = this.getPointData.bind(this)
    this.getPointVars = this.getPointVars.bind(this)
    this.getVarData = this.getVarData.bind(this)
    this.getLimitsData = this.getLimitsData.bind(this)
    this.getVarLimits = this.getVarLimits.bind(this)
    this.handleLimitCancel = this.handleLimitCancel.bind(this)
  }
  componentDidMount () {
    this.getPointData()
    // this.getVarData() // 默认显示所有变量
    this.getLimitsData()
  }
  getLimitsData () {
    const { monitorPointId, variableId } = this.state
    universalFetch(`${__SERVICE_API__}device/templates/monitors/limits?monitorPointId=${monitorPointId}` +
    `&variableId=${variableId}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          limitsData: json.result
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  getPointData = () => {
    universalFetch(`${__SERVICE_API__}device/templates/monitors/points`)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      this.setState({
        pointData: json.result
      })
      // pointData = json.result
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  getVarData () {
    const { monitorPointId } = this.state
    const url = `${__SERVICE_API__}device/templates/monitors/variables` +
      `?monitorPointId=${monitorPointId}`
    universalFetch(url)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          varData: json.result
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  getPointVars (point: Object) {
    const url = `${__SERVICE_API__}device/templates/monitors/variables` +
      `?monitorPointId=${point.id}`
    universalFetch(url)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          varData: json.result,
          monitorPointId: point.id
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  getVarLimits (data: Object) {
    universalFetch(`${__SERVICE_API__}device/templates/monitors/limits?` +
      `variableId=${data.id}&monitorPointId=${data.monitorPointId}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          visibleLimit: true,
          limitsData: json.result,
          variableId: data.id
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  handleLimitCancel () {
    this.setState({
      visibleLimit: false
    })
  }
  render () {
    const { pointData, varData, limitsData, visibleLimit } = this.state
    return (
      <div className={styles['main']}>
        <Point {...{
          pointData,
          refreshPoint: this.getPointData,
          getPointVars: this.getPointVars,
          getVarData: this.getVarData
        }} />
        <VarComp {...{
          varData,
          pointData,
          refreshVar: this.getVarData,
          refreshLimit: this.getVarLimits
        }} />
        <Modal
          title='阈值'
          visible={visibleLimit}
          onCancel={this.handleLimitCancel}
          footer={null}
          >
          <Limits {...{
            limitsData,
            refreshLimit: this.getLimitsData
          }} />
        </Modal>
      </div>
    )
  }
}
export default MonitorPage
